package cn.lijiguo.concurrency.part1.states;

/**
 * BlockedWaitingTimedWaiting
 * 展示 BLOCKED WAITING TIMED_WAITING 状态
 * @author lijiguo
 * @date 2020/1/6 14:50
 */
public class BlockedWaitingTimedWaiting implements Runnable {

    public static void main(String[] args) {
        BlockedWaitingTimedWaiting r = new BlockedWaitingTimedWaiting();

        Thread t1 = new Thread(r, "t1");
        Thread t2 = new Thread(r, "t2");

        t1.start();
        t2.start();

        System.out.println(t1.getState());
        System.out.println(t2.getState());

        try {
            Thread.sleep(1300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(t1.getState());
        System.out.println(t2.getState());
    }

    @Override
    public void run() {
        sync();
    }

    private synchronized void sync() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
